<template>
	<el-dialog v-model="dialog" :title="state.form.id ? '修改字典' : '添加字典'" width="60%" :before-close="handleClose">
		<el-form ref="formRef" :model="state.form" label-width="120px" :rules="state.rules" status-icon>
			<el-form-item label="字典标签" prop="dictLabel">
				<el-input v-model="state.form.dictLabel" show-word-limit maxlength="50"></el-input>
			</el-form-item>
			<el-form-item label="字典键值" prop="dictValue">
				<el-input v-model="state.form.dictValue" show-word-limit maxlength="100"></el-input>
			</el-form-item>
			<el-form-item label="备注" prop="remarks">
				<el-input v-model="state.form.remarks" show-word-limit type="textarea" maxlength="100"></el-input>
			</el-form-item>
			<el-form-item label="排序序号" prop="sort">
				<el-input-number v-model="state.form.sort" :min="0"></el-input-number>
			</el-form-item>
			<el-form-item label="回显样式" prop="showClass">
				<el-select v-model="state.form.showClass" placeholder="请选择回显样式">
					<el-option v-for="item in showClassList" :key="item.value" :label="item.label" :value="item.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-radio-group v-model="state.form.status">
					<el-radio label="0">正常</el-radio>
					<el-radio label="1">停用</el-radio>
				</el-radio-group>
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="handleClose">关 闭</el-button>
				<el-button type="primary" :loading="loading" @click="submitForm(formRef)">确 认</el-button>
			</span>
		</template>
	</el-dialog>
</template>
<script lang="ts" setup>
import { ref, reactive } from 'vue';
import { addObj, editObj, getById } from '/@/api/upms/dictvalue';
import { ElMessage, FormInstance } from 'element-plus';
const emit = defineEmits(['initPage']);
const state = reactive({
	form: { id: undefined, dictLabel: '', dictValue: '', remarks: '', status: '0', sort: 0, dictId: '', showClass: '' },
	rules: {
		dictLabel: [
			{
				required: true,
				message: '请输入字典标签',
				trigger: 'change',
			},
		],
		dictValue: [
			{
				required: true,
				message: '请输入字典键值',
				trigger: 'change',
			},
		],
		status: [
			{
				required: true,
				message: '请选择状态',
				trigger: 'change',
			},
		],
		sort: [
			{
				required: true,
				message: '请输入排序序号',
				trigger: 'change',
			},
		],
	},
});
const showClassList = ref([
	{
		value: 'default',
		label: '默认',
	},
	{
		value: 'primary',
		label: 'primary',
	},
	{
		value: 'success',
		label: 'success',
	},
	{
		value: 'info',
		label: 'info',
	},
	{
		value: 'warning',
		label: 'warning',
	},
	{
		value: 'danger',
		label: 'danger',
	},
]);
const dialog = ref(false);
const loading = ref(false);
const formRef = ref();
const initForm = (row: any) => {
	if (row && row.id) {
		getDetail(row.id);
	} else {
		state.form.dictId = row.dictId;
	}
	dialog.value = true;
};
const getDetail = (id: string) => {
	loading.value = true;
	// 修改
	getById(id)
		.then((response: any) => {
			loading.value = false;
			state.form = response.data.data;
		})
		.catch(() => {
			loading.value = false;
		});
};
/**
 * 关闭事件
 */
const handleClose = () => {
	resetForm(formRef.value);
};
/**
 * 重置表单
 */
const resetForm = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	loading.value = false;
	dialog.value = false;
	formEl.resetFields();
};
/**
 * 提交按钮
 */
const submitForm = async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid, fields) => {
		if (valid) {
			loading.value = true;
			if (state.form.id) {
				//修改
				edit();
			} else {
				//新增
				add();
			}
		}
	});
};
/**
 * 新增
 */
const add = () => {
	addObj(state.form)
		.then((response: any) => {
			resetForm(formRef.value);
			ElMessage.success('新增成功');
			emit('initPage');
		})
		.catch(() => {
			loading.value = false;
		});
};
/**
 * 修改
 */
const edit = () => {
	editObj(state.form)
		.then((response: any) => {
			resetForm(formRef.value);
			ElMessage.success('修改成功');
			emit('initPage');
		})
		.catch(() => {
			loading.value = false;
		});
};
defineExpose({
	initForm,
});
</script>
